import tkinter as tk
from tkinter import font
import socket

from about_and_help import *
from sender import *
from receive import *
from usertable import *
from securityset import *
from certificate import *

import threading
import asyncio

if __name__!="__main__":
	chatwin=tk.Tk()
	chatwin.geometry("1280x760")
	chatwin.title("SLSchat host: "+socket.gethostname())

	contentText=tk.Text(chatwin,undo=True,autoseparators=True,borderwidth='5')#聊天框
	contentscrolbar=tk.Scrollbar(chatwin,borderwidth='3')#聊天框的竖直滚动条
	contentText.config(yscrollcommand=contentscrolbar.set)
	contentscrolbar.config(command=contentText.yview)
	sendText=tk.Text(chatwin,borderwidth='5')#发送框
	sendscrolbar=tk.Scrollbar(chatwin,borderwidth='3')#发送框的滚动条
	sendText.config(yscrollcommand=sendscrolbar.set)
	sendscrolbar.config(command=sendText.yview)
	targetIPprompt=tk.Label(chatwin,text="当前目标主机")
	targetportprompt=tk.Label(chatwin,text="目标端口号")
	localIPprompt=tk.Label(chatwin,text="当前本机名义")

	localIPin=tk.Entry(chatwin,width="20",borderwidth='3')
	targetIPin=tk.Entry(chatwin,width="20",borderwidth='3')
	targetportin=tk.Entry(chatwin,width="20",borderwidth='3')
	targetportin.insert(0,"6544")

	#操作栏按钮
	connectbutton_send=tk.Button(chatwin,text="建立发送连接",command=lambda:makesendconnect_win(localIPin))
	connectbutton_recevice=tk.Button(chatwin,text="建立接收连接",command=lambda:makereceiveconnect_win())
	sendbutton=tk.Button(chatwin,text="发送文本消息",
		command=lambda:sendtext_by_button(sendText,contentText,targetIPin,targetportin))
	imagebutton=tk.Button(chatwin,text="发送图片内容",
		command=lambda:sendimage_by_button(contentText,targetIPin.get(),(int)(targetportin.get())))
	clearoutbutton=tk.Button(chatwin,text="清空\n输入",command=lambda:sendText.delete('1.0',tk.END))
	filebutton=tk.Button(chatwin,text="选择文件发送",
		command=lambda:sendfile_by_button(targetIPin.get(),int(targetportin.get()),contentText))
	recordbutton=tk.Button(chatwin,text="保存对话记录",command=lambda:savechatrecord(contentText))

	#设置栏按钮
	usertablebutton=tk.Button(chatwin,text="查看连线用户",command=lambda:putuseronline_table())
	sessionkeybutton=tk.Button(chatwin,text="设置总体密钥",command=lambda:setmainkey_win())
	askcertbutton=tk.Button(chatwin,text="申请获取证书",command=lambda:askcertificate_win())
	recvcertbutton=tk.Button(chatwin,text="接收证书申请",command=lambda:recvcertificate_win())
	aboutbutton=tk.Button(chatwin,text="关于",command=lambda:putabout())
	contentText.place(relx=0.01,rely=0.02,relwidth=0.7,relheight=0.6)
	contentscrolbar.place(relx=0.712,rely=0.02,relwidth=0.01,relheight=0.6)
	sendText.place(relx=0.01,rely=0.67,relwidth=0.7,relheight=0.2)
	sendscrolbar.place(relx=0.712,rely=0.67,relwidth=0.01,relheight=0.2)

	#操作栏布局
	localIPin.place(relx=0.8,rely=0.52)
	targetIPin.place(relx=0.8,rely=0.56)
	targetportin.place(relx=0.8,rely=0.6)
	localIPprompt.place(relx=0.73,rely=0.52)
	targetIPprompt.place(relx=0.73,rely=0.56)
	targetportprompt.place(relx=0.73,rely=0.6)
	connectbutton_recevice.place(relx=0.73,rely=0.65,relwidth=0.07)
	connectbutton_send.place(relx=0.81,rely=0.65,relwidth=0.07)
	sendbutton.place(relx=0.73,rely=0.7,relwidth=0.07)
	imagebutton.place(relx=0.81,rely=0.7,relwidth=0.07)
	clearoutbutton.place(relx=0.885,rely=0.65,relwidth=0.03,relheight=0.085)
	filebutton.place(relx=0.73,rely=0.75,relwidth=0.07)
	recordbutton.place(relx=0.81,rely=0.75,relwidth=0.07)
	localIPin.insert(0,socket.gethostname())#设置本机主机名为默认发送名义

	#设置栏布局
	usertablebutton.place(relx=0.01,rely=0.9,relwidth=0.07)
	sessionkeybutton.place(relx=0.085,rely=0.9,relwidth=0.07)
	askcertbutton.place(relx=0.165,rely=0.9,relwidth=0.07)
	recvcertbutton.place(relx=0.245,rely=0.9,relwidth=0.07)
	aboutbutton.place(relx=0.325,rely=0.9,relwidth=0.07)
	#设置聊天框中不同内容的字体特点
	textstyle_name=font.Font(family='KaiTi',size=16)#与自己连线的用户的名称
	textstyle_youself=font.Font(family='KaiTi',size=16)#自身
	textstyle_text=font.Font(size=13)#文本消息内容
	textstyle_file=font.Font(family='KaiTi',size=14)#文件传输
	contentText.tag_config('name',foreground='green',font=textstyle_name)
	contentText.tag_config('youself',foreground='pink',font=textstyle_youself)
	contentText.tag_config('text',font=textstyle_text)
	contentText.tag_config('file',foreground='blue',font=textstyle_file)

	def recvwork():
		while chatwin.winfo_exists()==True:#只要聊天窗口存在就一直在接收消息
			receivemessage(chatwin,contentText)
	
	def threadwork():
		recvth=threading.Thread(target=recvwork)#建立网络消息接收的线程
		recvth.setDaemon(True)#将其设置为守护线程，利于程序的正常关闭
		recvth.start()#因为mainloop是死循环彻底阻塞，故需要先开启其它线程最后启用mainloop
		chatwin.mainloop()